const { Controller } = require("egg");
// const axios = require("axios");

class ApiController extends Controller {
  //  获取微信列表
  async getWxList() {
    const { ctx, service } = this;
    const result = await service.weixindb.getWeChatList();
    ctx.body = {
      code: 0,
      msg: "",
      count: result.length,
      data: result,
    };
  }
  async getWxInfo() {
    const { ctx, service } = this;
    const wxid = ctx.params.wxid;
    try {
      const result = await service.weixindb.getWxInfo(wxid);

      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "获取微信信息成功",
        data: result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }
  async getGroupInfo() {
    const { ctx, service } = this;
    const { wxid, mywxid } = ctx.request.body;
    try {
      const result = await service.getinfo.queryObj(wxid, mywxid);
      // console.log(result.result);
      // 更新

      if (result.code === 200) {
        const field = {
          nick: result.result.nick,
          groupMemberNum: result.result.groupMemberNum,
          goupManger: result.result.goupManger,
          avatarMinUrl: result.result.avatarMinUrl,
          avatarMaxUrl: result.result.avatarMaxUrl,
        };
        const update = await service.wxgroup.editGroup(mywxid, wxid, field);
        // console.log('getGroupInfo', update);
        ctx.body = {
          code: 20000,
          status: "ok",
          msg: "获取成功",
          data: update,
        };
      }
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  async getPublickInfo() {
    const { ctx, service } = this;
    const { wxid, mywxid } = ctx.request.body;
    try {
      const result = await service.getinfo.queryObj(wxid, mywxid);
      // console.log(result.result);
      // 更新
      if (result.code === 200) {
        const field = {
          nick: result.result.nick,
          wxNum: result.result.wxNum,
          avatarMinUrl: result.result.avatarMinUrl,
          avatarMaxUrl: result.result.avatarMaxUrl,
        };
        await service.wxpublick.editPublick(mywxid, wxid, field);
      }
      // console.log(result);
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "获取成功",
        data: result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }
  // 获取好友列表
  async getFriendList() {
    const { ctx, service } = this;
    const wxid = ctx.params.wxid;
    const { field, page, pageSize } = ctx.request.body.data;
    // console.log(wxid, field, page, pageSize);
    const result = await service.wxfriend.getFriendList(
      wxid,
      field,
      page,
      pageSize
    );
    ctx.body = result;
  }
  // 获取群列表
  async getGroupList() {
    const { ctx, service } = this;
    const wxid = ctx.params.wxid;
    const { field, page, pageSize } = ctx.request.body.data;
    // console.log( wxid, field, page, pageSize)
    const result = await service.wxgroup.getGroupList(
      wxid,
      field,
      page,
      pageSize
    );

    ctx.body = result;
  }
  // 获取公众号列表
  async getPublickList() {
    const { ctx, service } = this;
    const wxid = ctx.params.wxid;
    const { field, page, pageSize } = ctx.request.body.data;
    const result = await service.wxpublick.getPublicList(
      wxid,
      field,
      page,
      pageSize
    );

    ctx.body = result;
  }
  // 更新微信列表
  async updateWxList() {
    const { ctx, service } = this;
    try {
      const result = await service.weixindb.updateWxList();
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "更新微信列表",
        data: result.result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }
  // 更新群列表
  async updateGroupList() {
    const { ctx, service } = this;
    const wxid = ctx.params.wxid;
    // console.log('updateGroupList');
    try {
      const result = await service.wxgroup.updateGroupList(wxid);
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "更新群列表",
        data: result.result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 更新好友列表
  async updateFriendList() {
    const { ctx, service } = this;
    const wxid = ctx.params.wxid;
    try {
      const result = await service.wxfriend.updateFrendList(wxid);
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "更新好友列表",
        data: result.result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 更新公众号列表
  async updatePublickList() {
    const { ctx, service } = this;
    const wxid = ctx.params.wxid;
    try {
      const result = await service.wxpublick.updatePublickList(wxid);
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "更新公众号列表",
        data: result.result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 修改好友字段
  async editFriend() {
    const { ctx, service } = this;
    const { mywxid, wxid, field } = ctx.request.body;

    try {
      const result = await service.wxfriend.editFriend(mywxid, wxid, field);
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "更新成功",
        data: result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  async editFriendRemark() {
    const { ctx, service } = this;
    const { mywxid, wxid, field } = ctx.request.body;
    try {
      const result = await service.wxfriend.editFriendRemark(
        mywxid,
        wxid,
        field
      );
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "更新成功",
        data: result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 批量修改好友字段
  async batchEditFriend() {
    const { ctx, service } = this;
    const { mywxid, wxids, field } = ctx.request.body;

    try {
      const results = await service.wxfriend.batchEditFriend(
        mywxid,
        wxids,
        field
      );
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "批量更新成功",
        data: {
          total: wxids.length,
          success: results.filter((r) => r).length,
        },
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 修改群聊字段
  async editGroup() {
    const { ctx, service } = this;
    const { mywxid, wxid, field } = ctx.request.body;

    try {
      const result = await service.wxgroup.editGroup(mywxid, wxid, field);
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "更新成功",
        data: result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 修改公众号字段
  async editPublick() {
    const { ctx, service } = this;
    const { mywxid, wxid, field } = ctx.request.body;

    try {
      const result = await service.wxpublick.editPublick(mywxid, wxid, field);
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "更新成功",
        data: result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 获取微信信息
  async getFriendInfo() {
    const { ctx, service } = this;
    const { wxid, mywxid } = ctx.request.body;

    try {
      const result = await service.getinfo.queryObj(wxid, mywxid);
      // console.log(result.result);
      // 更新
      if (result.code === 200) {
        const field = {
          nick: result.result.nick,
          wxNum: result.result.wxNum,
          remark: result.result.remark,
          sex: result.result.sex,
          v3: result.result.v3,
          city: result.result.city,
          avatarMinUrl: result.result.avatarMinUrl,
          avatarMaxUrl: result.result.avatarMaxUrl,
        };
        const resdata = await service.wxfriend.editFriend(mywxid, wxid, field);
        // console.log(resdata);
        ctx.body = {
          code: 20000,
          status: "ok",
          msg: "获取成功",
          data: resdata,
        };
      }
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 删除好友
  async deleteFriend() {
    const { ctx, service } = this;
    const { mywxid, wxids } = ctx.request.body;
    // Convert single wxid to array if needed
    const wxidsArray = Array.isArray(wxids) ? wxids : [wxids];

    try {
      const results = await Promise.all(
        wxidsArray.map((wxid) => service.wxfriend.deleteFriend(mywxid, wxid))
      );

      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "删除成功",
        data: {
          total: wxidsArray.length,
          success: results.filter((r) => r).length,
        },
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 更新微信字段
  async editWeixin() {
    const { ctx, service } = this;
    const { mywxid, field } = ctx.request.body;
    try {
      const result = await service.weixindb.editWeixin(mywxid, field);
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "更新成功",
        data: result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  async quitGroup() {
    const { ctx, service } = this;
    const { mywxid, wxids } = ctx.request.body;
    const wxidsArray = Array.isArray(wxids) ? wxids : [wxids];

    try {
      const results = await Promise.all(
        wxidsArray.map((wxid) => service.wxgroup.quitGroup(mywxid, wxid))
      );

      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "删除成功",
        data: {
          total: wxidsArray.length,
          success: results.filter((r) => r).length,
        },
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 获取群成员列表
  async getMemberList() {
    const { ctx, service } = this;
    const { groupwxid, selectedWxid } = ctx.request.body;
    
    try {
      const result = await service.wxchatroomdb.getMemberList(selectedWxid,groupwxid);      
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "获取成功",
        count: result.length,
        data: result,
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  // 批量开关处理
  async batchfriendswitch() {
    const { ctx } = this;
    const { mywxid, field } = ctx.request.body;
    try {
      await ctx.model.Wxfriend.updateMany({ ownerWxid: mywxid },{
        $set: field,
      })
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "批量开关处理成功",
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }

  async batchgroupswitch(){
    const { ctx } = this;
    const { mywxid, field } = ctx.request.body;
    // console.log(mywxid, field)
    try {
      await ctx.model.Wxgroup.updateMany({ ownerWxid: mywxid },{
        $set: field,
      })
      ctx.body = {
        code: 20000,
        status: "ok",
        msg: "批量开关处理成功",
      };
    } catch (error) {
      ctx.body = {
        code: 50000,
        status: "fail",
        msg: error.message,
      };
    }
  }
}

module.exports = ApiController;
